#! /usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
from std_msgs.msg import String
from std_msgs.msg import Int32
from std_msgs.msg import UInt8
import os
import sys
import time
import wave
import datetime
import pyaudio
from sound_play.libsoundplay import SoundClient
# from speech.msg import Description
from control.msg import gpsr_msg
from control.msg import sequence_msg

class fmm_speech(object):
    def __init__(self):
        #名字列表->两个位置
        self.name_list = ["feel","platform","plant","one","thirsty","want","beer","need","football","ball"]
        #["amelia","angel","charlie","ava","hunter","jack","charlotte","max",
                            #"noah","oliver","mia", "parker","sam","thomas","william", "olivia"]
        self.flag=1
        rospy.Subscriber("/xunfei_to_control", String, self.xfeiCallback)#订阅语音识别结果的话题
        # rospy.Subscriber("/iat_text", String, self.xfeiCallback)
        
        #rospy.Subscriber("/image/people_feature", Description, self.visDesCallback)
        #rospy.Subscriber("/image/people_position", String, self.visPosCallback)
        rospy.Subscriber("/gpsr/control", gpsr_msg, self.controlCallback)
        self.speech_pub_to_control = rospy.Publisher('/gpsr/control', gpsr_msg, queue_size=1)
        self.speech_pub_to_nav = rospy.Publisher('/gpsr/sequence', sequence_msg, queue_size=1)
        self.message = gpsr_msg()
        self.message.NeedHelp = False
        self.message.FinishState = False
        self.sequence_msg = sequence_msg()
        self.to_find_person_name_flag = 0

        self.to_find_person_name = None #要找的人的名字->要去的地方
        #self.person_information = None  #要找的人的信息
        self.sequence = 0 #表明位置的顺序

        #self.age = "unknown" 
        #self.gender = "unknown"
        #self.skin_color = "unknown"
        #self.hair_style = "unknown"
        #self.glasses = "unknown"
        #self.clothes_color = "unknown"
        #self.clothes = "unknown"
        #self.pose = "unknown"
        #self.position = "unknown"        

        self.sh = SoundClient(blocking = True)
        self.voice = rospy.get_param("~voice", "voice_kal_diphone")
        print("init ok")

    def xfeiCallback(self,msg):
        print("xfeiCallback")
        if msg.data.strip()=='':
            self.sh.say("I did not clearly hear you", self.voice)
            self.sh.say("please tell me again", self.voice)
            os.system("rostopic pub -1 /kws_data std_msgs/String 'jack'")#用jack唤醒
        else:
            string = msg.data
            symbols = ["!", "?", ".", ",", ";", ":"]
            output = []
            if string[-1] in symbols:
                string = string[:-1]
            for part in string.lstrip().split(","):
                for word in part.split():
                    for symbol in symbols:
                        if symbol in word:
                            word = word[:-1]
                    output.append(word)
            output = [item.lower() for item in output]
            print(output)

        for i in range(len(output)):
            if self.to_find_person_name_flag == 0:
                self.to_find_person_name = output[i]
            if output[i] in self.name_list:
                self.to_find_person_name_flag = self.to_find_person_name_flag + 1

        print(self.to_find_person_name_flag)
        print(self.to_find_person_name)
        if self.to_find_person_name_flag != 2:
            self.sh.say("sorry, please tell me again", self.voice)
            self.to_find_person_name_flag = 0
            os.system("rostopic pub -1 /kws_data std_msgs/String 'jack'")
        if (self.to_find_person_name_flag >= 1) and (self.to_find_person_name == "feel"or self.to_find_person_name == "thirsty" or self.to_find_person_name == "want" or self.to_find_person_name == "beer" ):
            print("desk")
            self.sh.say("yes,sir. i will go to desk to get the beer, and then give it to you", self.voice)
            self.sequence = 1
            self.message.NowTask = self.message.Receiving
            self.message.NextTask = self.message.GoToLoc1
            self.message.FinishState = True
            self.message.NeedHelp = False
            self.sequence_msg.seq = self.sequence_msg.desk
            self.speech_pub_to_nav.publish(self.sequence_msg)
            self.speech_pub_to_control.publish(self.message)
            os.system("rosnode kill /voiceRecognition")#关闭语音识别节点

        if (self.to_find_person_name_flag >= 1) and (self.to_find_person_name == "need" or self.to_find_person_name == "football"or self.to_find_person_name == "ball"):
            print("platform")
            self.sh.say("yes,sir. i will go to desk to get the football, and then give it to you", self.voice)
            self.sequence = 2
            self.message.NowTask = self.message.Receiving
            self.message.NextTask = self.message.GoToLoc1
            self.message.FinishState = True
            self.message.NeedHelp = False
            self.sequence_msg.seq = self.sequence_msg.platform
            self.speech_pub_to_nav.publish(self.sequence_msg)
            self.speech_pub_to_control.publish(self.message)
            os.system("rosnode kill /voiceRecognition")#关闭语音识别节点

    def controlCallback(self,msg):
        if msg.NowTask == self.message.Receiving and msg.FinishState == False:
            # self.sh.say("Dear operator, please talk to me after you call me jack", self.voice)
            self.sh.say("Waiting for Destination", self.voice)
            self.message.NowTask = msg.NowTask
            self.message.NextTask = msg.NextTask
            self.message.FinishState = False
            self.message.NeedHelp = False
            
        if msg.NowTask == self.message.Speaking1 and msg.FinishState == False and self.sequence == 1:
            self.sh.say("OK, i have got the beer at the desk, and i will go to platform to give it to you", self.voice)
            self.message.NowTask = msg.NowTask
            self.message.NextTask = msg.NextTask
            self.message.FinishState = True
            self.message.NeedHelp = False
            self.speech_pub_to_control.publish(self.message)    
        if msg.NowTask == self.message.Speaking1 and msg.FinishState == False and self.sequence == 2:
            self.sh.say("OK, i have got the football at the platform, and i will go to desk to give it to the guest", self.voice)
            self.message.NowTask = msg.NowTask
            self.message.NextTask = msg.NextTask
            self.message.FinishState = True
            self.message.NeedHelp = False
            self.speech_pub_to_control.publish(self.message)
        if msg.NowTask == self.message.Speaking2 and msg.FinishState == False:
            self.sh.say("Dear guest, here you are", self.voice)
            #self.information()              
            #self.sh.say(self.person_information, self.voice)
            self.sh.say("ok i have finished my work. Have fun!", self.voice)
            self.to_find_person_name = None
            #self.person_information = None
            os.system("gnome-terminal -x bash -c 'rosrun xfei_asr speech_recognition'")#开启语音识别节点
            rospy.sleep(1)
            os.system("rostopic pub -1 /kws_data std_msgs/String 'jack'")
            self.message.NowTask = msg.NowTask
            self.message.NextTask = msg.NextTask
            self.message.FinishState = True
            self.message.NeedHelp = False
            self.speech_pub_to_control.publish(self.message)
	    
        print("controlCallback")
        

   # def visDesCallback(self,msg):
	#if self.flag==1:
           # self.age = msg.age 
           # self.gender = msg.gender
           # self.skin_color = msg.skin_color
           # self.hair_style = msg.hair_style
           # self.glasses = msg.glasses
           # self.clothes_color = msg.clothes_color
           # self.clothes = msg.clothes
           # self.pose = msg.pose
	   # self.flag=0
        # 接受到消息则停止此任务

#my_change
        #task = findmymate()
        #task.NowTask = task.Recording
        #task.NextTask = task.EnterGate
        #task.FinishState = True
        #task.NeedHelp = False
        #self.speech_pub_to_control.publish(task)
#change_end

    #def visPosCallback(self,msg):
      #   self.position = msg.data    

   # def information(self):
      #  if self.gender == "male":
          #  self.person_information = "the guest is a " + self.gender + ", "
          #  if self.age != "unknown":
               # self.person_information = self.person_information + "he is about {} years old, ".format(self.age)
           # if self.skin_color != "unknown":
               # self.person_information = self.person_information + "his skin color is " + self.skin_color + ", "
           # if self.hair_style != "unknown":
               # self.person_information = self.person_information + "his hair style is " + self. hair_style + ", "
           # if self.glasses != "unknown":
               # if self.glasses != "none":
                   # self.glasses = "glasses"
               # self.person_information = self.person_information + "he is wearing " + self.glasses + ", "
           # if self.clothes_color != "unknown":
               # if self.clothes != "unknown":
                   # self.person_information = self.person_information + "he is wearing a " + self.clothes_color + " " + self.clothes + ", "
               # else:
                  #  self.person_information = self.person_information + "he dresses up in " + self.clothes_color + ", "
           # if self.pose == "sit":
               # self.person_information = self.person_information + "he is sitting on the " + self.position
          #  if self.pose == "stand":
               # self.person_information = self. person_information + "he is standing around the " + self.position
            
       # if self.gender == "female":
           # self.person_information = "the guest is a " + self.gender + ", "
           # if self.age != "unknown":
               # self.person_information = self.person_information + "she is about {} years old, ".format(self.age)
           # if self.skin_color != "unknown":
               # self.person_information = self.person_information + "her skin color is " + self.skin_color + ", "
          #  if self.hair_style != "unknown":
               # self.person_information = self.person_information + "her hair style is " + self. hair_style + ", "
          #  if self.glasses != "unknown":
              #  self.person_information = self.person_information + "she is wearing " + self.glasses + ", "
           # if self.clothes_color != "unknown":
               # if self.clothes != "unknown":
                  #  self.person_information = self.person_information + "she is wearing a " + self.clothes_color + " " + self.clothes + ", "
              #  else:
                 #   self.person_information = self.person_information + "she dresses up in " + self.clothes_color + " "
           # if self.pose == "sit":
               # self.person_information = self.person_information + "she is sitting on the " + self.position
          #  if self.pose == "stand":
              #  self.person_information = self. person_information + "she is standing around the " + self.position

if __name__ == '__main__':
    rospy.init_node('fmm_speech', anonymous=True)
    ctrl = fmm_speech()
    rospy.spin() 
